//
//  ImageUpLoadViewCtl.m
//  MoreFun
//
//  Created by Pro on 2018/4/30.
//  Copyright © 2018年 Pro. All rights reserved.
//

#import "ImageUpLoadViewCtl.h"

@interface ImageUpLoadViewCtl ()
@property (weak, nonatomic) IBOutlet UIImageView *AvatarImageView;

@end

@implementation ImageUpLoadViewCtl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"图片上传";
    NSString * avatarImageName = GETDEFAULTS(kUserAvatar)
    if (avatarImageName != nil) {
        [self.AvatarImageView sd_setImageWithURL:[NSURL URLWithString:avatarImageName] placeholderImage:[UIImage imageNamed:@"avatar"]];
    }
}
- (IBAction)changeAvatarImageAction:(UIButton *)sender {
     
}

/** wsx - 信息及设置周边 */
//重设图片
-(void)resetHeadImageView{
    //    NSLog(@"修改头像")
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"修改头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *camera = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self startUsingCamera];
        
    }];
    UIAlertAction *album = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self startUsingAlbum];
        
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:camera];
    [alert addAction:album];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}
//启用相机
-(void)startUsingCamera{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *pickerCtl = [[UIImagePickerController alloc]init];
        pickerCtl.delegate = self;
        pickerCtl.allowsEditing = YES;
        pickerCtl.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:pickerCtl animated:YES completion:nil];
    }
}
//启用相册
-(void)startUsingAlbum{
    UIImagePickerController *pickerCtl=[[UIImagePickerController alloc]init];
    pickerCtl.delegate = self;
    pickerCtl.allowsEditing = YES;
    pickerCtl.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:pickerCtl animated:YES completion:nil];
}
/** 压缩图片 */
-(UIImage *)thumbnaiWithImage:(UIImage *)image size:(CGSize)size{
    UIImage *newImage = nil;
    if (image!=nil) {
        //获取 要开始绘制的图片（尺寸大小）的上下文
        UIGraphicsBeginImageContext(size);
        //绘制size大小的图片
        [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
        //从现在绘制的图片 的上下文中 获取绘制好的图片
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        //关闭 绘制图片的上下文
        UIGraphicsEndImageContext();
    }
    return newImage;
}
/** 对imageView进行图片压缩，并给Imv赋值 */
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //获取 原始图片
    UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
    //进行图片压缩，尺寸
    UIImage *compressImage = [self thumbnaiWithImage:originalImage size:CGSizeMake(170, 170)];
    //将压缩的图片 转化为 NSData，以便上传 头像信息,缩小为0.1倍
    NSData *imageData = UIImageJPEGRepresentation(compressImage, 0.1);
//    SAVEDEFAULTS(imageData, TempImv)
    /**   注意！
     *   如果需要上传更新图片，在此处进行相应操作
     */
//    [ServerRequest resetHeadImv:^(BOOL state) {
//        [self viewWillAppear:YES];
//    }];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
